﻿using System;
using System.IO;
using System.Web;
using Bussiness.Model.Type;

namespace Bussiness.Utilities {
    public class DMUtil {
        public static readonly string LANG_EN_US = "en-US";
        public static readonly string LANG_VI_VN = "vi-VN";
        public static readonly string LANG_SESSION_KEY = "lang";

        public static readonly string NO_IMAGES_PATH = "~/Images/no_image.jpg";
        public static readonly string USER_FILE_PATH = "~/UserFiles/";
        public static readonly string USER_IMAGE_FILE_PATH = USER_FILE_PATH + "image/";
        public static readonly string USER_SLIDER_IMAGE_PATH = USER_FILE_PATH + "/slider/";

        public static string PhysicalApplicationPath { get { return HttpContext.Current.Request.PhysicalApplicationPath; } }

        public static Language String2Lang(string str) {
            if (str.Equals(LANG_EN_US)) return Language.en_US;
            return Language.vi_VI;
        }

        public static bool CheckExtensionImage(string imageName) {
            string extension = Path.GetExtension(imageName);
            if (extension != null) {
                string strExtension = extension.ToUpper();
                if (strExtension == ".JPG" || strExtension == ".GIF" || strExtension == ".JPEG" || strExtension == ".PNG" ||
                    strExtension == ".SWF")
                    return true;
            }
            return false;
        }

        public static string ImagePath(string path) {
            if (path == null) return NO_IMAGES_PATH;
            if (CheckExtensionImage(path)) {
                if (File.Exists(HttpContext.Current.Server.MapPath(path))) {
                    return path;
                }
            }
            return NO_IMAGES_PATH;
        }

        public static long ParseLong(string longValue) {
            try {
                return Int64.Parse(longValue);
            } catch (System.Exception) {
                return 0;
            }
        }

        public static void Message(string sMessage) {
            HttpContext.Current.Response.Write("<script language='javascript'>window.alert('" + sMessage +
                                               "');</script>");
        }

        public static void Message(string sMessage, string url) {
            HttpContext.Current.Response.Write("<script language='javascript'>window.alert('" + sMessage +
                                               "');window.location.href='" + url + "';</script>");
        }


    }
}